home *** CD-ROM | disk | FTP | other *** search
/ PCGUIA 117 / PC Guia 117.iso / Software / Produtividade / Software2 / Product4 / Setup.exe / drupal-4.6.0 / includes / pager.inc < prev    next >
Encoding:
Text File  |  2005-03-31  |  14.1 KB  |  417 lines

  1. <?php
  2. // $Id: pager.inc,v 1.42 2005/03/31 09:25:33 unconed Exp $
  3.  
  4. /**
  5.  * @file
  6.  * Functions to aid in presenting database results as a set of pages.
  7.  */
  8.  
  9. /**
  10.  * Perform a paged database query.
  11.  *
  12.  * Use this function when doing select queries you wish to be able to page. The
  13.  * pager uses LIMIT-based queries to fetch only the records required to render a
  14.  * certain page. However, it has to learn the total number of records returned
  15.  * by the query to compute the number of pages (the number of records / records
  16.  * per page). This is done by inserting "COUNT(*)" in the original query. For
  17.  * example, the query "SELECT nid, type FROM node WHERE status = '1' ORDER BY
  18.  * sticky DESC, created DESC" would be rewritten to read "SELECT COUNT(*) FROM
  19.  * node WHERE status = '1' ORDER BY sticky DESC, created DESC". Rewriting the
  20.  * query is accomplished using a regular expression.
  21.  *
  22.  * Unfortunately, the rewrite rule does not always work as intended for queries
  23.  * that already have a "COUNT(*)" or a "GROUP BY" clause, and possibly for
  24.  * other complex queries. In those cases, you can optionally pass a query that
  25.  * will be used to count the records.
  26.  *
  27.  * For example, if you want to page the query "SELECT COUNT(*), TYPE FROM node
  28.  * GROUP BY TYPE", pager_query() would invoke the incorrect query "SELECT
  29.  * COUNT(*) FROM node GROUP BY TYPE". So instead, you should pass "SELECT
  30.  * COUNT(DISTINCT(TYPE)) FROM node" as the optional $count_query parameter.
  31.  *
  32.  * @param $query
  33.  *   The SQL query that needs paging.
  34.  * @param $limit
  35.  *   The number of query results to display per page.
  36.  * @param $element
  37.  *   An optional integer to distinguish between multiple pagers on one page.
  38.  * @param $count_query
  39.  *   An SQL query used to count matching records.
  40.  * @param ...
  41.  *   A variable number of arguments which are substituted into the query (and
  42.  *   the count query) using printf() syntax. Instead of a variable number of
  43.  *   query arguments, you may also pass a single array containing the query
  44.  *   arguments.
  45.  * @return
  46.  *   A database query result resource, or FALSE if the query was not executed
  47.  *   correctly.
  48.  *
  49.  * @ingroup database
  50.  */
  51. function pager_query($query, $limit = 10, $element = 0, $count_query = NULL) {
  52.   global $pager_from_array, $pager_total;
  53.   $from = $_GET['from'];
  54.  
  55.   // Substitute in query arguments.
  56.   $args = func_get_args();
  57.   $args = array_slice($args, 4);
  58.   // Alternative syntax for '...'
  59.   if (is_array($args[0])) {
  60.     $args = $args[0];
  61.   }
  62.  
  63.   // Construct a count query if none was given.
  64.   if (!isset($count_query)) {
  65.     $count_query = preg_replace(array('/SELECT.*?FROM/As', '/ORDER BY .*/'), array('SELECT COUNT(*) FROM', ''), $query);
  66.   }
  67.  
  68.   // Convert comma-separated $from to an array, used by other functions.
  69.   $pager_from_array = explode(',', $from);
  70.  
  71.   if (count($args)) {
  72.     $pager_total[$element] = db_result(db_query($count_query, $args));
  73.     return db_query_range($query, $args, (int)$pager_from_array[$element], (int)$limit);
  74.   }
  75.   else {
  76.     $pager_total[$element] = db_result(db_query($count_query));
  77.     return db_query_range($query, (int)$pager_from_array[$element], (int)$limit);
  78.   }
  79. }
  80.  
  81. /**
  82.  * Format a query pager.
  83.  *
  84.  * Menu callbacks that display paged query results should call theme('pager') to
  85.  * retrieve a pager control so that users can view other results.
  86.  *
  87.  * @param $tags
  88.  *   An array of labels for the controls in the pager.
  89.  * @param $limit
  90.  *   The number of query results to display per page.
  91.  * @param $element
  92.  *   An optional integer to distinguish between multiple pagers on one page.
  93.  * @param $attributes
  94.  *   An associative array of query string parameters to append to the pager links.
  95.  * @return
  96.  *   An HTML string that generates the query pager.
  97.  *
  98.  * @ingroup themeable
  99.  */
  100. function theme_pager($tags = array(), $limit = 10, $element = 0, $attributes = array()) {
  101.   global $pager_total;
  102.   $output = '';
  103.  
  104.   if ($pager_total[$element] > $limit) {
  105.     $output .= '<div id="pager" class="container-inline">';
  106.     $output .= theme('pager_first', ($tags[0] ? $tags[0] : t('first page')), $limit, $element, $attributes);
  107.     $output .= theme('pager_previous', ($tags[1] ? $tags[1] : t('previous page')), $limit, $element, 1, $attributes);
  108.     $output .= theme('pager_list', $limit, $element, ($tags[2] ? $tags[2] : 9 ), '', $attributes);
  109.     $output .= theme('pager_next', ($tags[3] ? $tags[3] : t('next page')), $limit, $element, 1, $attributes);
  110.     $output .= theme('pager_last', ($tags[4] ? $tags[4] : t('last page')), $limit, $element, $attributes);
  111.     $output .= '</div>';
  112.  
  113.     return $output;
  114.   }
  115. }
  116.  
  117. /**
  118.  * @name Pager pieces
  119.  * @{
  120.  * Use these pieces to construct your own custom pagers in your theme. Note that
  121.  * you should NOT modify this file to customize your pager.
  122.  */
  123.  
  124. /**
  125.  * Format a "first page" link.
  126.  *
  127.  * @param $text
  128.  *   The name (or image) of the link.
  129.  * @param $limit
  130.  *   The number of query results to display per page.
  131.  * @param $element
  132.  *   An optional integer to distinguish between multiple pagers on one page.
  133.  * @param $attributes
  134.  *   An associative array of query string parameters to append to the pager links.
  135.  * @return
  136.  *   An HTML string that generates this piece of the query pager.
  137.  *
  138.  * @ingroup themeable
  139.  */
  140. function theme_pager_first($text, $limit, $element = 0, $attributes = array()) {
  141.   global $pager_from_array;
  142.   $output = '<div class="pager-first">';
  143.  
  144.   if ($pager_from_array[$element]) {
  145.     $output .= '<a href="'. pager_link(pager_load_array(0, $element, $pager_from_array), $element, $attributes) .'">'. $text .'</a>';
  146.   }
  147.   else {
  148.     $output .= ' ';
  149.   }
  150.   $output .= '</div>';
  151.   return $output;
  152. }
  153.  
  154. /**
  155.  * Format a "previous page" link.
  156.  *
  157.  * @param $text
  158.  *   The name (or image) of the link.
  159.  * @param $limit
  160.  *   The number of query results to display per page.
  161.  * @param $element
  162.  *   An optional integer to distinguish between multiple pagers on one page.
  163.  * @param $interval
  164.  *   The number of pages to move backward when the link is clicked.
  165.  * @param $attributes
  166.  *   An associative array of query string parameters to append to the pager links.
  167.  * @return
  168.  *   An HTML string that generates this piece of the query pager.
  169.  *
  170.  * @ingroup themeable
  171.  */
  172. function theme_pager_previous($text, $limit, $element = 0, $interval = 1, $attributes = array()) {
  173.   global $pager_from_array;
  174.   $output = '<div class="pager-previous">';
  175.   $from_new = pager_load_array(((int)$pager_from_array[$element] - ((int)$limit * (int)$interval)), $element, $pager_from_array);
  176.   if ($from_new[$element] < 1) {
  177.     $output .= theme('pager_first', $text, $limit, $element, $attributes);
  178.   }
  179.   else {
  180.     $output .= '<a href="'. pager_link($from_new, $element, $attributes) .'">'. $text .'</a>';
  181.   }
  182.   $output .= '</div>';
  183.   return $output;
  184. }
  185.  
  186. /**
  187.  * Format a "next page" link.
  188.  *
  189.  * @param $text
  190.  *   The name (or image) of the link.
  191.  * @param $limit
  192.  *   The number of query results to display per page.
  193.  * @param $element
  194.  *   An optional integer to distinguish between multiple pagers on one page.
  195.  * @param $interval
  196.  *   The number of pages to move forward when the link is clicked.
  197.  * @param $attributes
  198.  *   An associative array of query string parameters to append to the pager links.
  199.  * @return
  200.  *   An HTML string that generates this piece of the query pager.
  201.  *
  202.  * @ingroup themeable
  203.  */
  204. function theme_pager_next($text, $limit, $element = 0, $interval = 1, $attributes = array()) {
  205.   global $pager_from_array, $pager_total;
  206.   $output = '<div class="pager-next">';
  207.   $from_new = pager_load_array(((int)$pager_from_array[$element] + ((int)$limit * (int)$interval)), $element, $pager_from_array);
  208.   if ($from_new[$element] < $pager_total[$element]) {
  209.     $output .= '<a href="'. pager_link($from_new, $element, $attributes) .'">'. $text .'</a>';
  210.   }
  211.   else {
  212.     $output .= ' ';
  213.   }
  214.   $output .= '</div>';
  215.   return $output;
  216. }
  217.  
  218. /**
  219.  * Format a "last page" link.
  220.  *
  221.  * @param $text
  222.  *   The name (or image) of the link.
  223.  * @param $limit
  224.  *   The number of query results to display per page.
  225.  * @param $element
  226.  *   An optional integer to distinguish between multiple pagers on one page.
  227.  * @param $attributes
  228.  *   An associative array of query string parameters to append to the pager links.
  229.  * @return
  230.  *   An HTML string that generates this piece of the query pager.
  231.  *
  232.  * @ingroup themeable
  233.  */
  234. function theme_pager_last($text, $limit, $element = 0, $attributes = array()) {
  235.   global $pager_from_array, $pager_total;
  236.  
  237.   $output = '<div class="pager-last">';
  238.   $last_num = (($pager_total[$element] % $limit) ? ($pager_total[$element] % $limit) : $limit);
  239.   $from_new = pager_load_array(($pager_total[$element] - $last_num), $element, $pager_from_array);
  240.   if ($from_new[$element] < ($pager_from_array[$element] + $limit)) {
  241.     $output .= theme('pager_next', $text, $limit, $element, 1, $attributes);
  242.   }
  243.   else if (($from_new[$element] > $pager_from_array[$element]) && ($from_new[$element] > 0) && ($from_new[$element] < $pager_total[$element])) {
  244.     $output .= '<a href="'. pager_link($from_new, $element, $attributes) .'">'. $text .'</a>';
  245.   }
  246.   else {
  247.     $output .= ' ';
  248.   }
  249.   $output .= '</div>';
  250.   return $output;
  251. }
  252.  
  253. /**
  254.  * Format a summary of the current pager position, such as "6 through 10 of 52".
  255.  *
  256.  * @param $limit
  257.  *   The number of query results to display per page.
  258.  * @param $element
  259.  *   An optional integer to distinguish between multiple pagers on one page.
  260.  * @param $format
  261.  *   A printf-style format string for customizing the pager text.
  262.  * @return
  263.  *   An HTML string that generates this piece of the query pager.
  264.  *
  265.  * @ingroup themeable
  266.  */
  267. function theme_pager_detail($limit, $element = 0, $format = '%d through %d of %d.') {
  268.   global $pager_from_array, $pager_total;
  269.  
  270.   $output = '<div class="pager-detail">';
  271.   if ($pager_total[$element] > (int)$pager_from_array[$element] + 1) {
  272.     $output .= sprintf($format, (int)$pager_from_array[$element] + 1, ((int)$pager_from_array[$element] + $limit <= $pager_total[$element] ? (int)$pager_from_array[$element] + $limit : $pager_total[$element]), $pager_total[$element]);
  273.   }
  274.   $output .= '</div>';
  275.  
  276.   return $output;
  277. }
  278.  
  279. /**
  280.  * Format a list of nearby pages with additional query results.
  281.  *
  282.  * @param $limit
  283.  *   The number of query results to display per page.
  284.  * @param $element
  285.  *   An optional integer to distinguish between multiple pagers on one page.
  286.  * @param $quantity
  287.  *   The number of pages in the list.
  288.  * @param $text
  289.  *   A string of text to display before the page list.
  290.  * @param $attributes
  291.  *   An associative array of query string parameters to append to the pager links.
  292.  * @return
  293.  *   An HTML string that generates this piece of the query pager.
  294.  *
  295.  * @ingroup themeable
  296.  */
  297. function theme_pager_list($limit, $element = 0, $quantity = 5, $text = '', $attributes = array()) {
  298.   global $pager_from_array, $pager_total;
  299.  
  300.   $output = '<div class="pager-list">';
  301.   // Calculate various markers within this pager piece:
  302.   // Middle is used to "center" pages around the current page.
  303.   $pager_middle = ceil((int)$quantity / 2);
  304.   // offset adds "offset" second page
  305.   $pager_offset = (int)$pager_from_array[$element] % (int)$limit;
  306.   // current is the page we are currently paged to
  307.   if (($pager_current = (ceil(($pager_from_array[$element] + 1) / $limit))) < 1) {
  308.     $pager_current = 1;
  309.   }
  310.   // first is the first page listed by this pager piece (re quantity)
  311.   $pager_first = (int)$pager_current - (int)$pager_middle + 1;
  312.   // last is the last page listed by this pager piece (re quantity)
  313.   $pager_last = (int)$pager_current + (int)$quantity - (int)$pager_middle;
  314.   // max is the maximum number of pages content can is divided into
  315.   if (!$pager_max = (ceil($pager_total[$element] / $limit))) {
  316.     $pager_max = 1;
  317.   }
  318.   if ((int)$pager_offset) {
  319.     // adjust for offset second page
  320.     $pager_max++;
  321.     $pager_current++;
  322.   }
  323.   // End of marker calculations.
  324.  
  325.   // Prepare for generation loop.
  326.   $i = (int)$pager_first;
  327.   if ($pager_last > $pager_max) {
  328.     // Adjust "center" if at end of query.
  329.     $i = $i + (int)($pager_max - $pager_last);
  330.     $pager_last = $pager_max;
  331.   }
  332.   if ($i <= 0) {
  333.     // Adjust "center" if at start of query.
  334.     $pager_last = $pager_last + (1 - $i);
  335.     $i = 1;
  336.   }
  337.   // End of generation loop preparation.
  338.  
  339.   // When there is more than one page, create the pager list.
  340.   if ($i != $pager_max) {
  341.     $output .= $text;
  342.     if ($i > 1) {
  343.       $output .= '<div class="pager-list-dots-left">... </div>';
  344.     }
  345.  
  346.     // Now generate the actual pager piece.
  347.     for (; $i <= $pager_last && $i <= $pager_max; $i++) {
  348.       if ($i < $pager_current) {
  349.         $output .= theme('pager_previous', $i, $limit, $element, ($pager_current - $i), $attributes) ." ";
  350.       }
  351.       if ($i == $pager_current) {
  352.         $output .= '<strong>'. $i .'</strong> ';
  353.       }
  354.       if ($i > $pager_current) {
  355.         $output .= theme('pager_next', $i, $limit, $element, ($i - $pager_current), $attributes) ." ";
  356.       }
  357.     }
  358.  
  359.     if ($i < $pager_max) {
  360.       $output .= '<div class="pager-list-dots-right">...</div>';
  361.     }
  362.   }
  363.   $output .= '</div>';
  364.  
  365.   return $output;
  366. }
  367. /**
  368.  * @} End of "Pager pieces".
  369.  */
  370.  
  371. /**
  372.  * Format a link to a specific query result page.
  373.  *
  374.  * @param $from_new
  375.  *   The first result to display on the linked page.
  376.  * @param $element
  377.  *   An optional integer to distinguish between multiple pagers on one page.
  378.  * @param $attributes
  379.  *   An associative array of query string parameters to append to the pager link.
  380.  * @return
  381.  *   An HTML string that generates the link.
  382.  */
  383. function pager_link($from_new, $element, $attributes = array()) {
  384.   $q = $_GET['q'];
  385.   $from = array_key_exists('from', $_GET) ? $_GET['from'] : '';
  386.  
  387.   foreach ($attributes as $key => $value) {
  388.     $query[] = $key .'='. $value;
  389.   }
  390.  
  391.   $from_new = pager_load_array($from_new[$element], $element, explode(',', $from));
  392.   if (count($attributes)) {
  393.     $url = url($q, 'from='. implode($from_new, ',') .'&'. implode('&', $query));
  394.   }
  395.   else {
  396.     $url = url($q, 'from='. implode($from_new, ','));
  397.   }
  398.  
  399.   return check_url($url);
  400. }
  401.  
  402. function pager_load_array($value, $element, $old_array) {
  403.   $new_array = $old_array;
  404.   // Look for empty elements.
  405.   for ($i = 0; $i < $element; $i++) {
  406.     if (!$new_array[$i]) {
  407.       // Load found empty element with 0.
  408.       $new_array[$i] = 0;
  409.     }
  410.   }
  411.   // Update the changed element.
  412.   $new_array[$element] = (int)$value;
  413.   return $new_array;
  414. }
  415.  
  416. ?>
  417.